# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=mercurial
pkgver=6.6.1
pkgrel=0
pkgdesc="Scalable distributed SCM tool"
url="https://www.mercurial-scm.org/"
arch="all"
license="GPL-2.0-or-later"
depends="python3"
makedepends="
	cargo-auditable
	gettext-dev
	py3-gpep517
	py3-setuptools
	py3-wheel
	python3-dev
	zlib-dev
	"
checkdepends="
	bash
	diffutils
	py3-docutils
	sqlite
	unzip
	xz
	"
subpackages="
	$pkgname-pyc
	$pkgname-doc
	$pkgname-lang
	$pkgname-vim::noarch
	$pkgname-zsh-completion
	$pkgname-bash-completion
	"
source="https://www.mercurial-scm.org/release/mercurial-$pkgver.tar.gz
	blacklist.txt
	"
options="net !check"

case "$CARCH" in
aarch64|armhf)
	# around 400 of 919 tests time out
	options="$options !check"
	;;
esac
case "$CARCH" in
x86_64|armv7|armhf|aarch64|x86|ppc64le)
	# Oxidation, limited by Rust
	export HGWITHRUSTEXT=cpython
	makedepends="$makedepends cargo"
	subpackages="$subpackages $pkgname-chg"
	;;
esac

# secfixes:
#   4.9-r0:
#     - CVE-2019-3902

prepare() {
	default_prepare

	if [ "$HGWITHRUSTEXT" = "cpython" ]; then
		cd rust
		cargo fetch --target=$CTARGET --locked
		cd chg
		cargo fetch --target=$CTARGET --locked
	fi
}

build() {
	gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2

	if [ "$HGWITHRUSTEXT" = "cpython" ]; then
		cd rust
		cargo auditable build --release --frozen --manifest-path chg/Cargo.toml
	fi
}

check() {
	cd tests
	python3 run-tests.py --blacklist="$srcdir"/blacklist.txt
}

package() {
	python3 -m installer -d "$pkgdir" \
		.dist/*.whl

	install -Dm755 contrib/hgk contrib/hg-ssh hgeditor -t "$pkgdir"/usr/bin

	local man
	for man in doc/*.?; do
		install -Dm644 "$man" \
			"$pkgdir"/usr/share/man/man${man##*.}/${man##*/}
	done

	install -Dm644 contrib/vim/HGAnnotate.vim contrib/vim/hgtest.vim \
		-t "$pkgdir"/usr/share/vim/vimfiles/syntax/

	if [ "$HGWITHRUSTEXT" = "cpython" ]; then
		install -Dm755 rust/chg/target/release/chg -t "$pkgdir"/usr/bin/
	fi
}

chg() {
	pkgdesc="Client for Mercurial command server with cHg extension"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/bin/chg
}

vim() {
	depends=""
	pkgdesc="Vim syntax for $pkgname"
	install_if="vim $pkgname=$pkgver-r$pkgrel"

	amove usr/share/vim
}

lang() {
	pkgdesc="translations for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel lang"

	amove usr/lib/python3*/site-packages/mercurial/locale
}

sha512sums="
f16240f06a98a088e0229aa6584daa540eee92476a0a7934617c065c9f1012288cf3d6fb2d20fc7b247122bc360201f2e2c1c139c261a834f483bd09bef74d0c  mercurial-6.6.1.tar.gz
b24e1b79ca36ac1d78a5af4e35eb87581f209255838327a7d9315c17175d41bfa4b7f98c66be078cfa1e7d27a41c1a12bba265fa6d3d30edb4e2ba599fe0ed18  blacklist.txt
"
